package kai.rating;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.TextView;

public class Option extends Activity implements SeekBar.OnSeekBarChangeListener
{
	private static SeekBar sb_Kfactor;
	private static SeekBar sb_diff;
	private static SeekBar sb_block;
	private static TextView tv_Kfactor,tv_diff,tv_block;
	private static CheckBox Debug;
	private static Button setdefault;
	private static SharedPreferences pref;
	private static SharedPreferences.Editor edit;
	private static int Kfactor;
	private static int diff;
	private static int block;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.option);
		
		pref=getSharedPreferences("Elo",Activity.MODE_PRIVATE);
		edit=pref.edit();
		
		tv_Kfactor=(TextView) findViewById(R.id.VKfactor);
		tv_diff=(TextView) findViewById(R.id.Vdiff);
		tv_block=(TextView) findViewById(R.id.Vblock);
		sb_Kfactor=(SeekBar) findViewById(R.id.Kfactor);
		sb_diff=(SeekBar) findViewById(R.id.diff);
		sb_block=(SeekBar) findViewById(R.id.block);
		Debug=(CheckBox) findViewById(R.id.debug);
		setdefault=(Button) findViewById(R.id.setdefault);
		
		sb_Kfactor.setOnSeekBarChangeListener(this);
		sb_diff.setOnSeekBarChangeListener(this);
		sb_block.setOnSeekBarChangeListener(this);
		Debug.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
			@Override
			public void onCheckedChanged(CompoundButton button, boolean checked)
			{
				if(checked)
					edit.putBoolean("debug",true);
				else
					edit.putBoolean("debug",false);
				edit.commit();
			}
		});
		
		//sb_Kfactor.incrementProgressBy(50);
		
		refresh();
		
		setdefault.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v) {
				edit.clear();
				edit.commit();
				refresh();
			}
		});
	}
	private static void refresh()
	{
		Kfactor=pref.getInt("Kfactor",100);
		diff=pref.getInt("diff",10);
		block=pref.getInt("block",400);
		sb_Kfactor.setProgress((Kfactor/10)-1);
		sb_diff.setProgress(diff-1);
		sb_block.setProgress((block/10)-1);
		Debug.setChecked(pref.getBoolean("debug",false));
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
	{
		if(seekBar==sb_Kfactor)
			tv_Kfactor.setText(Integer.toString((progress+1)*10));
		else if(seekBar==sb_diff)
			tv_diff.setText(Integer.toString(progress+1));
		else if(seekBar==sb_block)
			tv_block.setText(Integer.toString((progress+1)*10));
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar)
	{		
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		if(seekBar==sb_Kfactor)
			edit.putInt("Kfactor",(seekBar.getProgress()+1)*10);
		else if(seekBar==sb_diff)
			edit.putInt("diff",seekBar.getProgress()+1);
		else if(seekBar==sb_block)
			edit.putInt("block",(seekBar.getProgress()+1)*10);
		edit.commit();
		
	}
	
	@Override
	public void onBackPressed()
	{
		setResult(RESULT_OK);
		finish();
	}
}